<template>
    <div>
        <h1>房型修改</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>房型名称</td>
                    <td><input type="text" v-model="info.rootname" /></td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                        <input type="file" @change="getphoto" />
                        <img :src="info.photo" alt="" width="100" height="100">
                    </td>
                </tr>
                <tr>
                    <td>数量</td>
                    <td><input type="text" v-model="info.num" /></td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" v-model="info.state" :value="1" name="state" />是
                        <input type="radio" v-model="info.state" :value="0" name="state" />否
                    </td>
                </tr>
                <tr>
                    <td>开始时间</td>
                    <td><input type="date" v-model="info.datetime" /></td>
                </tr>
                <tr>
                    <td>结束时间</td>
                    <td><input type="date" v-model="info.update" /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="修改" @click="ok" /></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { onMounted,reactive,ref } from 'vue';
import { useRoute,useRouter } from 'vue-router';
import axios from 'axios'
import moment from 'moment'

const route=useRoute();
const router=useRouter();

const info=reactive({
    id:route.query.id,
    rootname:'',
    photo:'',
    num:'',
    state:1,
    datetime:'',
    update:'',
})
//钩子函数
onMounted(()=>{
    FanTian();
})

//反填
const FanTian=()=>{
    axios({
        url:'/api/Root/FanTian',
        method:'get',
        params:{
            id:route.query.id
        }
    })
    .then(res=>{
        console.log(res);
        info.rootname=res.data.rootname;
        info.photo=res.data.photo;
        info.num=res.data.num;
        info.state=res.data.state;
        info.datetime=moment(res.data.datetime).format('YYYY-MM-DD');
        info.update=moment(res.data.update).format('YYYY-MM-DD');
    })
}



//图片
const getphoto=(e:any)=>{
    let file=e.target.files[0];
    let formdata=new FormData();
    formdata.append('file',file);
    axios({
        url:'/api/Imgurl/GetImgurl',
        method:'post',
        data:formdata
    })
    .then(res=>{
        console.log(res);
        info.photo=res.data;
    })
}

//修改
const ok=()=>{
    axios({
        url:'/api/Root/Update',
        method:'put',
        data:info
    })
    .then(res=>{
        if(res.data>0){
            alert('修改成功!');
            router.push('/RootList');
        }
        else{
            alert('修改失败!');
        }
    })
}


</script>